<?php

class ContentLogicComponent extends Object
{
	/**
	 *
	 * Save post
	 * @param array $data
	 * 			$data[ContentAuthor]
	 * 			$data[ContentPost]
	 */
	public function savePost($data) {
		Utils::loadModels($this, array('Content.ContentPost','Content.ContentAuthor'));
		// try to get the author
		$author = $this->ContentAuthor->findByEmail($data['ContentAuthor']['email']);
		if(!empty($author)){
			$data['ContentAuthor'] = $author['ContentAuthor'];
		} else {
			$result = $this->ContentAuthor->saveIt($data, 'email');
			if($result['status']){
				return $result;
			}
		}
		$data['ContentPost']['author_id'] = $data['ContentAuthor']['id'];
		$result = $this->ContentPost->saveIt($data);
		return $result;
	}

	public function viewPost($hash){
		Utils::loadModels($this, array('Content.ContentPost','Content.ContentAuthor'));
		$post = $this->ContentPost->findBySeoMd5Raw($hash);
		return $post;
	}
}